package com.example.seckill.Controller;

import com.example.seckill.annotation.CurrentUser;
import com.example.seckill.pojo.User;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("")
//作为Controller类的注解
//允许跨域，同时允许使用cookie

public class CaptchaController {

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @Autowired

    RedisTemplate redisTemplate;

//    @GetMapping("/captcha")
//    public void showOrderCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        // 生成验证码文本
//        System.out.println("captcha");
//        String captchaText = defaultKaptcha.createText();
//        System.out.println("code= "+captchaText);
//
//        // 将验证码文本存储在会话中，以供后续验证
////        request.getSession().setAttribute("captcha", captchaText);
////        System.out.println("setAttribute");
////        System.out.println("sessionID= "+request.getSession().getId());
//
//        //验证码存储在redis中
//        System.out.println("key");
//        redisTemplate.opsForValue().set("orderCaptcha",captchaText);
//
//        // 生成验证码图片
//        BufferedImage captchaImage = defaultKaptcha.createImage(captchaText);
//
//        // 将验证码图片写入响应
//        response.setContentType("image/png");
//
//        try (OutputStream outputStream = response.getOutputStream()) {
////            System.out.println(captchaImage);
//            ImageIO.write(captchaImage, "png", outputStream);
//            outputStream.flush();
//
//        }


//    @RequestMapping("/captcha")
//    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        CaptchaUtil.out(request, response);
//
//}
@RequestMapping("/captcha/{room_id}")
public void captcha(HttpServletRequest request, HttpServletResponse response,@CurrentUser User user, @PathVariable String room_id) throws Exception {
    // 设置请求头为输出图片类型
  response.setContentType("image/gif");
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);

    // 三个参数分别为宽、高、位数
    ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
    // 设置字体
    captcha.setFont(Captcha.FONT_10);
//    specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32));  // 有默认字体，可以不用设置
    // 设置类型，纯数字、纯字母、字母数字混合,TYPE_ONLY_NUMBER是纯数字
//    specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
//    captcha.getArithmeticString();
    // 存入redis
    redisTemplate.opsForValue().set("captcha:"+user.getUsername()+":"+room_id,captcha.text().toLowerCase(),300, TimeUnit.SECONDS);
//    request.getSession().setAttribute("captcha", captcha.text());

    try{
    // 输出图片流
    captcha.out(response.getOutputStream());
}catch(IOException e){
    log.error("输出验证码失败");
    }
}



    }
